// 玩家手牌
class playHandPoker extends egret.Sprite{
    public pointId:number;//保存位置ID
    private SeatType:number;
    private cardNum:number;
    public constructor(_seatType:number=1){
        super();
        this.SeatType = _seatType;//座位类型
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddEd,this);
    }
    private onAddEd(){
        this.addChild(this.winPokerBg);
        this.addChild(this.handPokerBg);
        this.addChild(this.qishouhuPokerBg);
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
    }
    private onRemove(){
    }

    private handPokerBg:egret.Sprite = new egret.Sprite();//手牌容器
    public qishouhuPokerBg:egret.Sprite = new egret.Sprite();//手牌容器
    private winPokerBg:egret.Sprite = new egret.Sprite();//赢牌容器
    private handPokerList:Array<any> = new Array();
    private winPokerList:Array<any> = new Array();
    private getPoker:any;//摸的
    private winPokerX:number;
    private winPokerY:number;
    private addNumber:number = 0;
    //右边
    private rightOrientation(){
        let lslength:number = this.handPokerList.length;
        this.handPokerList.reverse();//倒序排序
        for(var i=0;i<lslength;i++){
            this.handPokerList[i].y =27*i;
            this.handPokerBg.addChild(this.handPokerList[i]);
            this.handPokerBg.height = 27*i;
        }

        for(var i=this.winPokerList.length;i>0;i--){
            this.winPokerList[i-1].y = (104*i);
            this.winPokerBg.height  = (104*(i+1));
            this.winPokerList[i-1].scaleX = this.winPokerList[i-1].scaleY = 1.2;
        }
        this.winPokerBg.y = this.handPokerBg.y +(27*(lslength-1))-35-this.addNumber;

        if(this.getPoker){
            this.getPoker.y = this.handPokerBg.y - 45;
        }    
    }
    //对面
    private topOrientation(){
        this.handPokerList.reverse();//倒序排序
        for(var i=0;i<this.winPokerList.length;i++){
            this.winPokerList[i].x = (118*i);
            this.winPokerList[i].scaleX = this.winPokerList[i].scaleY = 1.2;
        }
        
        let lslength:number = this.handPokerList.length;
        for(var i=0;i<lslength;i++){
            this.handPokerList[i].x = 39*i;
        }

        this.winPokerBg.x = (39*lslength)+10;
        this.winPokerBg.y = 3;

        if(this.getPoker){
            this.getPoker.x =-(this.getPoker.width + 10);
        }
    }
    //左边
    private leftOrientation(){
        this.winPokerBg.height  = 0;
        for(var i=0;i<this.winPokerList.length;i++){
            this.winPokerList[i].y = (104*i);
            this.winPokerBg.height  = (104*(i+1));
            this.winPokerList[i].scaleX = this.winPokerList[i].scaleY = 1.2;
        }
        this.winPokerBg.y = 0;

        for(var i=0;i<this.handPokerList.length;i++){
            this.handPokerList[i].y = 27*i;
            this.handPokerBg.height = 27*i;
        }
        this.handPokerBg.y = this.winPokerBg.y+this.winPokerBg.height+5;
        
        if(this.getPoker){
            this.getPoker.y = (27*(i+1))+5-this.addNumber;
            this.handPokerBg.addChild(this.getPoker);
            //this.getPoker.y = this.handPokerBg.y+this.handPokerBg.height;
        }
    }
    /****摊牌-名牌****/
    public openDeal(msg,lastGetCard){
        if(!msg||msg.length<1) return;
        switch(this.SeatType){
            case 1:
            this.addhandPokerDeal("gamePoker_2_png",msg,lastGetCard);
            this.rightOrientation();
            break;

            case 2:
            this.addhandPokerDeal("gamePoker_3_png",msg,lastGetCard);
            this.topOrientation();
            break;

            case 3:
            this.addhandPokerDeal("gamePoker_4_png",msg,lastGetCard);
            this.leftOrientation();
            break;
        }
    }
    public addhandPokerDeal(picName:string,msg,lastGetCard){
        this.handPokerList = [];
        for(var i=this.handPokerBg.numChildren;i>0;i--){
            this.handPokerBg.removeChildAt(i-1);
        }
        
        this.getPoker = null;
        this.getPoker = new handPokerPlay(this.SeatType);
        this.getPoker.setContent(msg[i].suitType+"_"+msg[i].count+"_png");
        this.getPoker.visible = false;
        this.handPokerBg.addChild(this.getPoker);
        
        let lslast:any;
            for(var i=0;i<msg.length;i++){
               /* let lsobj:handPokerPlay = new handPokerPlay(this.SeatType);
                lsobj.setContent(msg[i].suitType+"_"+msg[i].count+"_png");
                this.handPokerBg.addChild(lsobj);
                this.handPokerList.push(lsobj);*/

                if(lastGetCard&&!lslast&&msg[i].cardIndex==lastGetCard.cardIndex){
                    lslast = lastGetCard;                    
                    //this.cardMove.visible = true;
                    //alert("aa");
                }else{
                    let lsobj:handPokerPlay = new handPokerPlay(this.SeatType);
                    
                    lsobj.setContent(msg[i].suitType+"_"+msg[i].count+"_png");
                    this.handPokerBg.addChild(lsobj);
                    this.handPokerList.push(lsobj);
                    //this.addSinglePokerDeal(msg[i].suitType,msg[i].count,msg[i].cardIndex,false);
                } 
            }   
            //this.pokerArrayAscending();
           // this.updateSize();
            if(lslast&&lastGetCard){
                this.addNumber = 30;
                this.addSinglePokerDeal(lastGetCard.suitType,lastGetCard.count,lastGetCard.cardIndex,true);
            }else{
                this.addNumber = 0;
            }
    }
    private pokerNum:number = 0;//添加过的牌数量
    private hideCred:any;    
    public addSinglePokerDeal(flower:string,value:string,id:number,grab:boolean=false){      
       
       if(flower&&value&&id){     
            let pokerMc:handPokerPlay = new handPokerPlay(this.SeatType); 
            pokerMc.scaleX = pokerMc.scaleY = 1.8;
            pokerMc.name = "poker_"+this.pokerNum;//this.handPokerList.length;
            this.pokerNum ++;
            pokerMc.setContent(flower+"_"+value+"_png");
            this.handPokerBg.addChild(pokerMc);
            
            if(grab){
                this.getPoker.visible = true;
                this.getPoker.setContent(flower+"_"+value+"_png");
                pokerMc.visible = false;
                this.hideCred = pokerMc;
                //pokerMc.x = pokerMc.width*this.handPokerList.length+10;
                //this.handPokerBg.width = pokerMc.width*(this.handPokerList.length+1);
                this.handPokerList.push(pokerMc);
                this.updateSize();
            }else{                
                //pokerMc.x = pokerMc.width*this.handPokerList.length;
                //this.handPokerBg.width = pokerMc.width*(this.handPokerList.length+1);                
                this.handPokerList.push(pokerMc);
            }
        }
    }
    //牌值排序
    /*public pokerArrayAscending(){         
        var lsArray:Array<any> = this.handPokerList.slice();
        var newArray:Array<any> = new Array();//排列红中
        var i:number=lsArray.length-1, j:number, temp:any;
        for(var m:number=0; m<lsArray.length; m++){  
            for(var n:number=lsArray.length-1; n > m; n--){  
                 if(lsArray[n].data.cardIndex!=32&&lsArray[n-1].data.cardIndex > lsArray[n].data.cardIndex){
                     temp = lsArray[n-1];         
                     lsArray[n-1] = lsArray[n];                                 
                     lsArray[n] = temp;
                 }  
            }  
        }  
        this.handPokerList = lsArray;
    }*/
    /**************摊牌 End***************/
    

    /*添加手牌*/
    private lastlastGetCard: any
    public setContent(amount:number,lastGetCard=null,msg=null){
        this.cardNum = amount;
        this.lastlastGetCard = lastGetCard;
        switch(this.SeatType){
            case 1:
            this.addhandPoker("gamePoker_2_png",amount,lastGetCard,msg);
            this.rightOrientation();
            break;

            case 2:
            this.addhandPoker("gamePoker_3_png",amount,lastGetCard,msg);
            this.topOrientation();
            break;

            case 3:
            this.addhandPoker("gamePoker_4_png",amount,lastGetCard,msg);
            this.leftOrientation();
            break;
        }
    }
    
    private addhandPoker(picName:string,amount:number,lastGetCard,msg=null){  
        this.cardNum = amount;
        this.handPokerList = [];
        //this.winPokerList = [];
        for(var i=this.handPokerBg.numChildren;i>0;i--){
            this.handPokerBg.removeChildAt(i-1);
        }
        this.getPoker = null;
        this.getPoker = MyLibrary.createBitmapByName(picName);
        this.getPoker.visible = false;
        this.handPokerBg.addChild(this.getPoker);
            for(var i=0;i<amount;i++){
                let lsobj:egret.Bitmap = MyLibrary.createBitmapByName(picName);
                this.handPokerBg.addChild(lsobj);
                this.handPokerList.push(lsobj);
            }
        
        if(lastGetCard){
            if(this.getPoker.visible == false&&this.handPokerList.length == amount){
                this.deletePoker(1);
            }
            this.addSinglePoker();//显示接的牌
        }
    }
    /*接一张牌*/
    public addSinglePoker(){
       if(this.getPoker){
        //    StageData.soundObj.soundSinglePlay("getCard");
           this.getPoker.visible = true;    
       }
    }
    /*打一张牌*/
    public deleteSinglePoker(){
       if(this.getPoker){
           if(this.getPoker.visible == true){
               this.getPoker.visible = false;
           }else{
               this.deletePoker(1);
           }
       }
       this.dispatchEventWith(PokerEvent.OUT_POKER,false,
                {x:this.getPoker.x,y:this.getPoker.y,doubleClick:true,value:null});
    }
    /*删除单张或多张牌*/
    public deletePoker(amount:number=1){
        if(amount>this.handPokerList.length-1){
           amount=this.handPokerList.length-1;
        }
        for(var i=0;i<amount;i++){
            this.handPokerBg.removeChild(this.handPokerList[0]);
            this.handPokerList.splice(0,1);
        }
        this.updateSize();
    }
     //清除所有牌
    public clearAllPoker(){
        for(var i=this.handPokerBg.numChildren;i>0;i--){
            this.handPokerBg.removeChildAt(i-1);
        }
        for(var i=this.winPokerBg.numChildren;i>0;i--){
            this.winPokerBg.removeChildAt(i-1);
        }
        this.handPokerList = [];
        this.winPokerList = [];
        if(this.getPoker){
           this.getPoker.visible = false;
       }
    }
    /*刷新手牌位置*/
    private updateSize(){
        switch(this.SeatType){
            case 1:
            this.rightOrientation();
            break;

            case 2:
            this.topOrientation();
            break;

            case 3:
            this.leftOrientation();
            break;
        }
    }
    private lastWinPokerList = []
    //刷新赢牌列表-碰牌/杠(花色，值，ID，赢牌的碰杠类型)
    public winPokerUpdateList(pokerList:Array<any>){  
        const hasAngang = pokerList.some(poker => poker.actionType === 'AN_GANG')
        if (_.isEqual(this.lastWinPokerList, pokerList) && !hasAngang) {
            console.log('winPokerUpdateList isEqual');
            return 
        } else {
            this.lastWinPokerList = pokerList
        }
        for(var i=this.winPokerBg.numChildren;i>0;i--){
            this.winPokerBg.removeChildAt(i-1);
        }
        this.winPokerList = [];
        console.log('pokerList', pokerList);
        for(var i=0;i<pokerList.length;i++){
            const id = pokerList[i].actionCode
            if (id === 7) {
                // this.qishouhula(pokerList[i])
                continue 
            }
            const chidepai = pokerList[i].actionCards ? pokerList[i].actionCards[0].count : null
            if (pokerList[i].actionCard) {
                this.winPoker(pokerList[i].actionCard.suitType,pokerList[i].actionCard.count,pokerList[i].actionCard.cardIndex,GameData.winPokerCode[pokerList[i].actionCode], chidepai);
            }
        }
    }

    // public qishouhula(data) {
    //     this.winPokerBg.visible = false
    //     const user = GameData.userData.find(user => user.seatIndex === data.seatIndex)
    //     console.log('other qishouhu ', user);
    // }

    public winPoker(flower:string,value:string,id:number,type:string, chidepai?: number){
        if (type === 'hu') {
        return
        }
        if(flower&&value){
            if(type!="peng"&&type!="chi"){
                //清除已经碰了的相同牌   
                for(var i=0;i<this.winPokerList.length;i++){
                    if(this.winPokerList[i].name == flower+value+"_peng"){                        
                        this.winPokerBg.removeChild(this.winPokerList[i]);
                        this.winPokerList.splice(i,1);
                    }
                }
            }

            let lsobj:any;
            if(this.SeatType == 1){
                lsobj = new winRight();
            }
            if(this.SeatType == 2){
                lsobj = new winTop();
            }
            if(this.SeatType == 3){
                lsobj = new winLeft();
            }
            lsobj.name = flower+value+"_"+type;
            lsobj.setContent("X"+flower+"_"+value+"_png",type, chidepai);
            this.winPokerList.push(lsobj);
            this.winPokerBg.addChild(lsobj);
            this.updateSize();  

            // if(type == "peng"){
            //     this.deletePoker(2);
            // } 
            // if(type == "gang"){
            //     this.deletePoker(3);
            // }    
            // if(type == "gonggang"){
            //     this.deletePoker(1);
            // }        
            // if(type == "angang"){
            //     this.deletePoker(1);
            // } 
            // if(type == "chi"){
            //     this.deletePoker(2);
            // }               
        }
    }

    public qishouhula(msg) {
        console.log('qishouhulaother');
        this.handPokerBg.visible = false
        this.qishouhuPokerBg.removeChildren()
        // const type = msg.action.actionCardMap[0].type
        const showAll = msg.action.actionCardMap.some(map => map.type === 3 || map.type === 4)
        const showToAllData = msg.action.actionCardMap.find(map => map.type === 3 || map.type === 4)
        const notShowToAllData = msg.action.actionCardMap.filter(map => map.type === 1 || map.type === 2)
        if (showAll) {
            const tanpaiGroup = new eui.Group()
            if (this.SeatType === 1 || this.SeatType === 3) {
                var vLayout:eui.VerticalLayout = new eui.VerticalLayout();
                vLayout.gap = -5
                vLayout.horizontalAlign = egret.HorizontalAlign.CENTER;
                tanpaiGroup.layout = vLayout; //设置问垂直布局
            } else {
                var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
                hLayout.gap = 0
                hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
                tanpaiGroup.layout = hLayout
            }

            showToAllData.cards.forEach((data, index) => {
                // const {suitType, count, cardIndex} = data
                const pai = new TanpaiOther(this.SeatType)
                pai.x = index * pai.width
                pai.setData(data)
                tanpaiGroup.addChild(pai)
                setTimeout(() => {
                    this.qishouhuPokerBg.removeChildren()
                    this.handPokerBg.visible = true
                }, 8000)
            })
            this.qishouhuPokerBg.addChild(tanpaiGroup)
        } else {
            const tanpaiGroup = new eui.Group()
            if (this.SeatType === 1 || this.SeatType === 3) {
                var vLayout:eui.VerticalLayout = new eui.VerticalLayout();
                vLayout.gap = -5
                vLayout.horizontalAlign = egret.HorizontalAlign.CENTER;
                tanpaiGroup.layout = vLayout; //设置问垂直布局
            } else {
                var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
                hLayout.gap = 0
                hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
                tanpaiGroup.layout = hLayout
            }

            notShowToAllData.forEach((notshowtoAll, index) => {
                setTimeout(() => {
                    notshowtoAll.cards.forEach((data, index2) => {
                        // const {suitType, count, cardIndex} = data
                        const pai = new TanpaiOther(this.SeatType)
                        pai.x = index2 * pai.width
                        pai.setData(data)
                        tanpaiGroup.addChild(pai)
                        setTimeout(() => {
                            // 展示完以后就移除 显示正常手牌  如果有下一种胡牌则移除子牌
                            if (index === notShowToAllData.length - 1) {
                                this.qishouhuPokerBg.removeChildren()
                                this.handPokerBg.visible = true 
                            } else {
                                tanpaiGroup.removeChildren()
                            }
                        }, 3500)
                    })  
                    if (notshowtoAll.cards.length < GameData.myUserData.handCount) {
                        const addBackNum = GameData.myUserData.handCount - notshowtoAll.cards.length
                        for (let i = 0; i < addBackNum; i++) {
                            let pai = new TanpaiOther(this.SeatType)
                            pai.x = (i + notshowtoAll.cards.length - 1) * pai.width
                            pai.setBack()
                            tanpaiGroup.addChild(pai)
                        }
                    }
                }, index * 4000)
            })
            this.qishouhuPokerBg.addChild(tanpaiGroup)
        }
    } 
}